function [thtau_grid, P_thtau, cdfu, cdfc] = thetaproc(match_qual, m_th, n_th)

% This function creates the grid of the stochastic process of match quality
% and the transition probabilities. 

sigma_eps=match_qual(2)^0.5; 
if match_qual(1)< 1
    meanth=0/(1-match_qual(1)); 
    sdth=sigma_eps/((1-match_qual(1)^2)^(0.5));
else
    meanth=0; 
    sdth=sigma_eps;
end

thtau_grid=zeros(1,n_th);
thtau_grid(1)=meanth - m_th*sdth;
thtau_grid(n_th)=meanth + m_th*sdth; 

dth=(thtau_grid(n_th)-thtau_grid(1))/(n_th-1); 

for i=2:n_th-1;
    thtau_grid(i)=thtau_grid(i-1) + dth;
end

P_thtau=zeros(n_th,n_th); %

for i=1:n_th 
P_thtau(i,1)=normcdf((thtau_grid(1)+0.5*dth-match_qual(1)*thtau_grid(i))/sigma_eps); 
end

for i=1:n_th 
    P_thtau(i,n_th)=1-normcdf((thtau_grid(n_th)-0.5*dth-match_qual(1)*thtau_grid(i))/sigma_eps);  
end

for i=1:n_th 
    for j=2:n_th-1
        P_thtau(i,j)=normcdf((thtau_grid(j)+0.5*dth-match_qual(1)*thtau_grid(i))/sigma_eps) -normcdf((thtau_grid(j)-0.5*dth-match_qual(1)*thtau_grid(i))/sigma_eps);
    end
end

cdfu=zeros(1,n_th); 

cdfu(1,n_th)=1; 

for i=1:n_th-1 
cdfu(1,i)=normcdf((thtau_grid(i)-0.5*dth)/sigma_eps); 
end

cdfc=zeros(n_th,n_th); 

for i=1:n_th 
    pastcdf=0; 
    for j=1:n_th 
        currentcdf=pastcdf + P_thtau(i,j);
        cdfc(i,j)=currentcdf; 
        pastcdf=currentcdf;
    end
end

end

